package com.kgcx.schedule.controller;

import com.kgcx.common.client.order.pojo.Order;
import com.kgcx.common.mqtt.TopicConst;
import com.kgcx.common.utils.AuroraPush;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.schedule.client.CityClient;
import com.kgcx.schedule.rabbitmq.consumer.OrderChangedConsumer;
import com.kgcx.schedule.rabbitmq.consumer.OrderCreatedConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 袁进勇
 *
 */
@Api(value = "调度测试接口", tags = "调度测试接口")
@RestController
@RequestMapping(value = "/v1/schedule/testing")
public class ScheduleTestController extends SuperController {
    private final Logger log = LoggerFactory.getLogger(ScheduleTestController.class);

    @Resource
    private OrderCreatedConsumer orderCreatedConsumer;
    @Resource
    private OrderChangedConsumer orderChangedConsumer;
    @Resource
    private CityClient cityClient;

    @ApiOperation(value = "模拟订单创建的MQ消息", httpMethod = "POST", notes = "")
    @RequestMapping(value = "/kgcxScheduleOrderCreateQueue", method = RequestMethod.POST)
    public void kgcxScheduleOrderCreateQueue(@RequestBody Order order) {
        orderCreatedConsumer.orderCreated(JsonUtil.toString(order));
    }

    @ApiOperation(value = "模拟订单取消的MQ消息", httpMethod = "POST", notes = "")
    @RequestMapping(value = "/kgcxScheduleOrderCancelQueue", method = RequestMethod.POST)
    public void kgcxScheduleOrderCancelQueue(@RequestBody Order order) {
        orderChangedConsumer.orderCancelled(JsonUtil.toString(order));
    }

    @ApiOperation(value = "模拟行程开始的MQ消息", httpMethod = "POST", notes = "")
    @RequestMapping(value = "/kgcxScheduleOrderStartQueue", method = RequestMethod.POST)
    public void kgcxScheduleOrderStartQueue(@RequestBody Order order) {
        orderChangedConsumer.orderStarted(JsonUtil.toString(order));
    }

    @ApiOperation(value = "模拟行程结束的MQ消息", httpMethod = "POST", notes = "")
    @RequestMapping(value = "/kgcxScheduleOrderFinishQueue", method = RequestMethod.POST)
    public void kgcxScheduleOrderFinishQueue(@RequestBody Order order) {
        orderChangedConsumer.orderFinished(JsonUtil.toString(order));
    }

    @ApiOperation(value = "模拟推送账单的MQ消息", httpMethod = "POST", notes = "")
    @RequestMapping(value = "/kgcxSchedulePushBillQueue", method = RequestMethod.POST)
    public void kgcxSchedulePushBillQueue(@RequestBody Order order) {
        orderChangedConsumer.pushBill(JsonUtil.toString(order));
    }

    @RequestMapping(value = "/notifyDriverByMqttJpush", method = RequestMethod.POST)
    @ApiOperation(value = "模拟推送派单信息给司机", httpMethod = "POST", notes = "")
    public void notifyDriverByMqttJpush(
            @ApiParam(name = "driver_id", value = "司机ID", required = true) @RequestParam Long driver_id, //
            @ApiParam(name = "order_no", value = "订单号", required = true) @RequestParam String order_no //
    ) {
        // 发送通知
        AuroraPush.pushTargetForDriver(String.valueOf(driver_id), "您接到一笔新的派车订单，请及时为乘客进行服务。");

        // 发送消息
        AuroraPush.pushTargetMsgForDriver(TopicConst.SCHEDULE_ORDER_SUCCESSFUL, order_no, String.valueOf(driver_id),
                AuroraPush.PLATFORM_ANDROID);
    }

//    @ApiOperation(value = "模拟为服务调用", httpMethod = "GET", notes = "")
//    @RequestMapping(value = "/getCity", method = RequestMethod.GET)
//    public ReturnData<City> getCity( //
//            @ApiParam(name = "cityId", value = "城市ID", required = true) @RequestParam Long cityId //
//    ) {
//        try {
//            City city = FeignUtil.invoke(() -> cityClient.getCity(cityId));
//            return new ReturnData<City>(city);
//        } catch (BusinessException e) {
//            log.error(e.getMessage(), e);
//            return new ReturnData<City>(String.valueOf(e.getCode()), e.getMessage());
//        }
//    }
}
